<?php
$jsMethodName = "rm_insurances_change_".$this->unit->id;
$jsCompulsoryMethodName = "rm_insurance_compulsory".$this->unit->id;
$jsInsurancesName = "rm_insurance_".$this->unit->id;
$jsSelectedInsurancesName = "rm_selected_insurances_".$this->unit->id;
$jsCompulsoryInsurancesName = "rm_compulsory_insurances_".$this->unit->id;
?>
<?php foreach ($this->rows as $insurance) { ?>
<tr>
    <td colspan="2">&nbsp;</td>
    <td>
        <?php echo $insurance['name'].' ('.$insurance['type'].')'; ?>
    </td>
    <td>
    <?php echo $this->currencySymbol; ?> <?php echo $insurance['value']; ?>&nbsp;*
    <?php if ($insurance['max'] > 1) { ?>
        <select autocomplete="off" id="rm_insurances[<?php echo $this->unit->id; ?>][<?php echo $insurance['id']; ?>]" name="rm_insurances[<?php echo $this->unit->id; ?>][<?php echo $insurance['id']; ?>]" onchange="<?php echo $jsMethodName; ?>(<?php echo $insurance['id']; ?>, this.value);" >
            <?php if (!$insurance['compulsory']) { ?>
                <option value="0">0</option>
                <?php for ($i = $insurance['min']; $i <= $insurance['max']; $i++) { ?>
                    <option value="<?php echo $i; ?>"><?php echo $i;?></option>
                <?php } ?>
            <?php } else { ?>
                <?php for ($i = $insurance['min']; $i <= $insurance['max']; $i++) { ?>
                    <option value="<?php echo $i; ?>" <?php if ($i == $insurance['min'] ) { echo 'selected="selected"'; } ?> ><?php echo $i;?></option>
                <?php } ?>
            <?php } ?>
        </select>
    <?php } else { ?>
        <?php if ($insurance['compulsory']) { ?>
            <input
                type="hidden"
                id="rm_insurances[<?php echo $this->unit->id; ?>][<?php echo $insurance['id']; ?>]"
                name="rm_insurances[<?php echo $this->unit->id; ?>][<?php echo $insurance['id']; ?>]"
                value="1"
            /> 1
        <?php } else { ?>
            <input
                type="checkbox"
                id="rm_insurances[<?php echo $this->unit->id; ?>][<?php echo $insurance['id']; ?>]"
                name="rm_insurances[<?php echo $this->unit->id; ?>][<?php echo $insurance['id']; ?>]"
                value="1"
                onchange="<?php echo $jsMethodName; ?>(<?php echo $insurance['id']; ?>, this.checked);"
            />
        <?php } ?>
    <?php } ?>
    </td>
    <td align="right">
        <?php echo $this->currencySymbol; ?> <span id="rm_insurances_subtotal[<?php echo $this->unit->id; ?>][<?php echo $insurance['id']; ?>]">0.00</span>
    </td>
    <td></td>
</tr>
<?php } ?>

<script type="text/javascript">
var <?php echo $jsInsurancesName; ?> = <?php echo Zend_Json::encode($this->rows); ?>;
var <?php echo $jsSelectedInsurancesName; ?> = [];
var <?php echo $jsCompulsoryInsurancesName; ?> = <?php echo Zend_Json::encode($this->compulsoryRows)?>;
function <?php echo $jsMethodName; ?>(insurance_id, value) {
    var insurancePrice = 0;

    for(var i = 0; i < <?php echo $jsInsurancesName; ?>.length; i++) {
        if (insurance_id == <?php echo $jsInsurancesName; ?>[i].id) {
            insurancePrice = <?php echo $jsInsurancesName; ?>[i].value;
            document.getElementById('rm_insurances_subtotal[<?php echo $this->unit->id; ?>]['+insurance_id+']').innerHTML = Ext.util.Format.number(<?php echo $jsInsurancesName; ?>[i].value * value, '0.00');
            break;
        }
    }

    var currentInsuranceChange = 0;
    for(var i = 0; i < <?php echo $jsSelectedInsurancesName; ?>.length; i++) {
        if (<?php echo $jsSelectedInsurancesName; ?>[i].id == insurance_id) {
            currentInsuranceChange = (value - <?php echo $jsSelectedInsurancesName; ?>[i].value) * insurancePrice;
            <?php echo $jsSelectedInsurancesName; ?>[i].value = value;
            for (var i = 0; i < rm_total.length; i++) {
                if (rm_total[i].unitID == <?php echo $this->unit->id; ?>) {
                    rm_total[i].total += currentInsuranceChange;
                    break;
                }
            }
            rm_show_total();
            return true;
        }
    }

    <?php echo $jsSelectedInsurancesName; ?>.push({
        id: insurance_id,
        value: value
    });

    for (var i = 0; i < rm_total.length; i++) {
        if (rm_total[i].unitID == <?php echo $this->unit->id; ?>) {
            rm_total[i].total += insurancePrice * value;
            break;
        }
    }

    rm_show_total();
}
function <?php echo $jsCompulsoryMethodName; ?>(){
    for(var i = 0; i < <?php echo $jsCompulsoryInsurancesName; ?>.length; i++){
        <?php echo $jsMethodName; ?>(<?php echo $jsCompulsoryInsurancesName; ?>[i].id, <?php echo $jsCompulsoryInsurancesName; ?>[i].min);
    }
}
rm_total_listeners.push(<?php echo $jsCompulsoryMethodName; ?>);
</script>